﻿#include "SelectDatatDialog.h"

#include <QIcon>
#include <ElaText.h>
#include <QVBoxLayout>
#include <ElaComboBox.h>
#include <ElaScrollArea.h>
#include "Curve2DKeySelcetBar.h"

namespace pst
{
    SelectDatatDialog::SelectDatatDialog(QWidget* parent)
        : ElaWidget(parent)
        , m_variableAxisValue{ nullptr }
        , m_keySelectBarList{}
    {
        setWindowTitle("数据选择");
        setWindowIcon(QIcon(":/include/Image/Moon.jpg"));
        this->setIsFixedSize(true);
        setContentsMargins(0, 0, 0, 0);
        this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        //setWindowModality(Qt::ApplicationModal);//设置为模态，点击其他控件不响应
        setWindowButtonFlags(ElaAppBarType::CloseButtonHint);
    }

    void SelectDatatDialog::setDialogTitle(const QString& title)
    {
        setWindowTitle(title);
    }

    QString SelectDatatDialog::getDialogTitle() const
    {
        return windowTitle();
    }

    void SelectDatatDialog::setKeyDataList(
        const QList<QPair<QString, QVector<double>>>& dataList)
    {
        m_keyDataList = dataList;
    }

    QList<QPair<QString, QVector<double>>>
        SelectDatatDialog::getkeyDataList() const
    {
        return m_valueDataList;
    }

    void SelectDatatDialog::setValueDataList(
        const QList<QPair<QString, QVector<double>>>& dataList)
    {
        m_valueDataList = dataList;
    }

    QList<QPair<QString, QVector<double>>> SelectDatatDialog::getValueDataList() const
    {
        return QList<QPair<QString, QVector<double>>>();
    }

    void SelectDatatDialog::updateUI()
    {
        auto _page1Layout = new QVBoxLayout(this);

        // 变量区域
        auto variableArea = new ElaScrollArea(this);
        auto variableAreaLayout = new QHBoxLayout(variableArea);
        ElaText* listText = new ElaText("变量", this);
        //listText->setTe xtPixelSize(12);
        m_variableAxisValue = new ElaComboBox(this);
        variableAreaLayout->addWidget(listText);
        variableAreaLayout->addWidget(m_variableAxisValue);
        _page1Layout->addWidget(variableArea);



        QWidget* _otherKeyWidget = new QWidget(this);
        //otherKey->setMaximumWidth(_contentWidth);
        _otherKeyWidget->setStyleSheet("background:transparent;");

        ElaScrollArea* otherKeyArea = new ElaScrollArea(this);
        otherKeyArea->setWidget(_otherKeyWidget);
        otherKeyArea->setWidgetResizable(true);
        //otherKeyArea->setFixedHeight(100);
        otherKeyArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        otherKeyArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        auto otherKeyAreaAreaLayout = new QVBoxLayout(_otherKeyWidget);
        int index = 0;
        for (const auto& i : m_keyDataList)
        {
            if (m_optionalKeyList.isEmpty()) {
                // 变量区
                m_variableAxisValue->addItem(i.first);
            }
            else if (m_optionalKeyList.contains(i.first)) {
                // 变量区
                m_variableAxisValue->addItem(i.first);
                m_indexMapping.append(index);
            }
            ++index;
            // 其他值区域
            auto cur = new  Curve2DKeySelcetBar(this);
            cur->setData(i.first, i.second.toList());
            otherKeyAreaAreaLayout->addWidget(cur);
            m_keySelectBarList.push_back(cur);
        }
        otherKeyAreaAreaLayout->addStretch();
        _page1Layout->addWidget(otherKeyArea);

        //其他参数--> value
        QWidget* _valueWidget = new QWidget(this);
        //otherKey->setMaximumWidth(_contentWidth);
        _valueWidget->setStyleSheet("background:transparent;");
        ElaScrollArea* valueyArea = new ElaScrollArea(this);
        valueyArea->setWidget(_valueWidget);
        valueyArea->setWidgetResizable(true);
        //otherKeyArea->setFixedHeight(100);
        valueyArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        valueyArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        auto valueAreaAreaLayout = new QVBoxLayout(valueyArea);
        _page1Layout->addWidget(valueyArea);

        QList<QString> valueList;
        for (auto i : m_valueDataList)
        {
            valueList.append(i.first);
        }
        m_valueSelectBar = new  Curve2DKeySelcetBar(this);
        m_valueSelectBar->setData("其他参数", valueList);
        otherKeyAreaAreaLayout->addWidget(m_valueSelectBar);


        /// 步骤很重要，先设置m_variableAxisValue的值，再连信号，再设置currentIndex
        connect(m_variableAxisValue, QOverload<int>::of(&ElaComboBox::currentIndexChanged),
            this, &SelectDatatDialog::slot_variableIndexChanged);
        if (!m_keyDataList.empty())
        {
            m_variableAxisValue->setCurrentIndex(0);
            int tmpIndex = 0;
            if (!m_indexMapping.isEmpty()) {
                tmpIndex = m_indexMapping.at(tmpIndex);
            }
            m_keySelectBarList.at(tmpIndex)->hide();
        }
        //if (!m_valueDataList.empty())
        //{
        //    m_valueSelectBar->hide();
        //}
        _page1Layout->addStretch();

        auto _cancelOKLayout = new QHBoxLayout();
        ElaPushButton* cancelButton = new ElaPushButton(this);
        ElaPushButton* okButton = new ElaPushButton(this);
        cancelButton->setText("取消");
        okButton->setText("确定");
        _cancelOKLayout->addStretch();
        _cancelOKLayout->addWidget(cancelButton);
        _cancelOKLayout->addWidget(okButton);
        _page1Layout->addLayout(_cancelOKLayout);

        connect(cancelButton, &ElaPushButton::clicked, this, [this]() {this->close(); });
        connect(okButton, &ElaPushButton::clicked, this, &SelectDatatDialog::slot_sendChosenInfo);
    }

    void SelectDatatDialog::slot_variableIndexChanged(int index)
    {
        if (!m_indexMapping.isEmpty()) {
            index = m_indexMapping.at(index);
        }
        for (int i = 0; i < m_keySelectBarList.size(); ++i)
        {
            i == index ? m_keySelectBarList.at(i)->hide() : m_keySelectBarList.at(i)->show();
        }
    }

    void pst::SelectDatatDialog::slot_sendChosenInfo()
    {
        auto variableIndex = m_variableAxisValue->currentIndex();
        if (!m_indexMapping.isEmpty()) {
            variableIndex = m_indexMapping.at(variableIndex);
        }
        QList<QPair<int, QList<int>>> chosenkeyList;
        for (int i = 0; i < m_keySelectBarList.size(); ++i)
        {
            if (i == variableIndex) continue;
            auto indexs = m_keySelectBarList.at(i)->getSelectedIndex();
            chosenkeyList.append(qMakePair(i, indexs));
        }
        auto valueIndexs = m_valueSelectBar->getSelectedIndex();
        this->close();
        emit signal_sendSelectedInfo(variableIndex, chosenkeyList, valueIndexs);
    }
}
